package com.smart.campus.school.service.impl;

import java.util.List;
import com.smart.campus.common.utils.DateUtils;
import com.smart.campus.school.domain.ExamAnswer;
import com.smart.campus.school.mapper.ExamAnswerMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.smart.campus.school.mapper.ExamQuestionMapper;
import com.smart.campus.school.domain.ExamQuestion;
import com.smart.campus.school.service.IExamQuestionService;
import com.smart.campus.common.core.text.Convert;

/**
 * 试题Service业务层处理
 * 
 * @author Zhihe.Li
 * @date 2020-05-20
 */
@Service
public class ExamQuestionServiceImpl implements IExamQuestionService 
{
    @Autowired
    private ExamQuestionMapper examQuestionMapper;
    @Autowired
    private ExamAnswerMapper examAnswerMapper;

    /**
     * 查询试题
     * 
     * @param id 试题ID
     * @return 试题
     */
    @Override
    public ExamQuestion selectExamQuestionById(Long id)
    {
        ExamAnswer examAnswer = new ExamAnswer();
        examAnswer.setQuestionId(id);
        List<ExamAnswer> examAnswerList = examAnswerMapper.selectExamAnswerList(examAnswer);
        ExamQuestion question = examQuestionMapper.selectExamQuestionById(id);
        question.setAnswerList(examAnswerList);
        return question;
    }

    /**
     * 查询试题列表
     * 
     * @param examQuestion 试题
     * @return 试题
     */
    @Override
    public List<ExamQuestion> selectExamQuestionList(ExamQuestion examQuestion)
    {
        return examQuestionMapper.selectExamQuestionList(examQuestion);
    }

    /**
     * 新增试题
     * 
     * @param examQuestion 试题
     * @return 结果
     */
    @Override
    public int insertExamQuestion(ExamQuestion examQuestion)
    {
        examQuestion.setCreateTime(DateUtils.getNowDate());
        examQuestion.setStatus(1);
        int id = examQuestionMapper.insertExamQuestion(examQuestion);
        List<ExamAnswer> examAnswerList = examQuestion.getAnswerList();
        if(CollectionUtils.isNotEmpty(examAnswerList)){
            for (ExamAnswer examAnswer : examAnswerList) {
                examAnswer.setQuestionId(examQuestion.getId());
                examAnswer.setCreateTime(examQuestion.getCreateTime());
                examAnswer.setCreateBy(examQuestion.getCreateBy());
                examAnswerMapper.insertExamAnswer(examAnswer);
            }
        }
        return id;
    }

    /**
     * 修改试题
     * 
     * @param examQuestion 试题
     * @return 结果
     */
    @Override
    public int updateExamQuestion(ExamQuestion examQuestion)
    {
        examQuestion.setUpdateTime(DateUtils.getNowDate());
        List<ExamAnswer> examAnswerList = examQuestion.getAnswerList();
        examAnswerMapper.deleteExamAnswerByQuestionId(examQuestion.getId());
        if(CollectionUtils.isNotEmpty(examAnswerList)){
            for (ExamAnswer examAnswer : examAnswerList) {
                examAnswer.setQuestionId(examQuestion.getId());
                examAnswer.setCreateTime(examQuestion.getCreateTime());
                examAnswer.setCreateBy(examQuestion.getCreateBy());
                examAnswerMapper.insertExamAnswer(examAnswer);
            }
        }
        return examQuestionMapper.updateExamQuestion(examQuestion);
    }

    /**
     * 删除试题对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteExamQuestionByIds(String ids)
    {
        return examQuestionMapper.deleteExamQuestionByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除试题信息
     * 
     * @param id 试题ID
     * @return 结果
     */
    @Override
    public int deleteExamQuestionById(Long id)
    {
        return examQuestionMapper.deleteExamQuestionById(id);
    }
}
